﻿using MediatR;
using W.Purchase.API.Application.Command.PayableCommand;
using W.Purchase.Repository.Impl;
using W.Purchase.Repository.Interface;

namespace W.Purchase.API.Application.CommandHandler.PayableCommandHandler
{
    public class PayableUpdCommandHandler:IRequestHandler<PayableUpdCommand,int>
    {
        private readonly IPayableRepository _ipayableRepository;
        public PayableUpdCommandHandler(IPayableRepository payableRepository)
        {
            _ipayableRepository = payableRepository;
        }

        public async Task<int> Handle(PayableUpdCommand request, CancellationToken cancellationToken)
        {
            var list = await _ipayableRepository.GetAllAsaync();
            var plist = list.Where(c => c.PayableId == request.PayableId).FirstOrDefault();
            if (plist != null)
            {
                plist.PayableId = request.PayableId;
                plist.PayableSerialNumber = request.PayableSerialNumber;
                plist.PayableName = request.PayableName;
                plist.FinanceTypeName = request.FinanceTypeName;
                plist.EndDate = request.EndDate;
                plist.Payer = request.Payer;
                plist.Payee = request.Payee;
                plist.PayableMoney = request.PayableMoney;
                plist.ActuallyPaidMoney = request.ActuallyPaidMoney;
                plist.ResidueMoney = request.ResidueMoney;
                plist.Status = request.Status;
                plist.PaymentNum = request.PaymentNum;
                plist.Receivableremark = request.Receivableremark;
                plist.office_id = request.office_id;
                plist.user_id = request.user_id;
                plist.CreateName = request.CreateName;
                plist.CreateTime = request.CreateTime;
                plist.UpdateName = request.UpdateName;
                plist.UpdateTime = DateTime.Now;
                plist.IsDelete = false;
            }
            return await _ipayableRepository.Update(plist);
        }
    }
}
